package cc.utils;

import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

public class LanguageParser extends DefaultHandler
{
	HashMap<String, String> mapResult = new HashMap<String, String>();
	public LanguageParser()
	{
		mapResult.clear();
	}
	
	public void startElement (String uri, String localName, String rawName, Attributes attributes)
    {
		if (attributes.getLength ()  == 2)
		{
			String strKey = attributes.getValue (0);
			String strVal = attributes.getValue(1);
			mapResult.put(strKey, strVal);
		}
		else if((attributes.getLength() != 2) && (localName.equalsIgnoreCase("keyword")))
			System.out.println("Error in XML format");
    }
	
	public void endDocument() throws SAXException
	{
		Iterator itr = mapResult.keySet().iterator();
		while(itr.hasNext()) 
		{
			String key = (String) itr.next();
		    System.out.println("<"+ key + ", " + mapResult.get(key) + ">");
		}
	}
	
	public static void main(String args[])
	{
		LanguageParser handler = new LanguageParser();
	
		try 
		{
            SAXParserFactory spf = SAXParserFactory.newInstance ();
            SAXParser parser = spf.newSAXParser ();

              parser.parse("D:\\webApp\\CC\\src\\cc\\utils\\sample.xml", handler);
        }
        catch (Exception ex) 
        {
                    System.out.println(ex);
        }
	}
}
